package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.GroupDto;
import com.tcm.entity.dto.param.GroupCreateParam;
import com.tcm.entity.resp.R;
import com.tcm.service.GroupService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 学生学习小组控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/9/11 21:20
 */
@RestController
@RequestMapping("/stu/group")
public class StuGroupController {

    @Autowired
    private GroupService groupService;

    /**
     * 创建学习小组
     * @param param 小组创建参数
     * @param request 请求对象
     * @return 创建结果，true为成功，false为小组已到最大数量
     */
    @RequestMapping(method = RequestMethod.POST)
    public R createGroup(@RequestBody GroupCreateParam param, HttpServletRequest request){
        return groupService.createGroup(param, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.GROUP_JOIN_COUNT_MAX);
    }

    /**
     * 获取学习小组列表
     * @param request 请求对象
     * @return 学习小组列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R getGroupList(HttpServletRequest request){
        return R.success(groupService.getGroupList(AccessTokenUtil.getUserAccount(request)));
    }

    /**
     * 获取小组邀请码
     * @param groupId 学习小组ID
     * @return 邀请码
     */
    @RequestMapping(value = "/invite", method = RequestMethod.GET)
    public R getGroupInvitationCode(@RequestParam Long groupId){
        Map<String, String> code = groupService.getGroupInvitationCode(groupId);

        return code != null ? R.success(code) : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 获取邀请小组信息
     * @param code 邀请码
     * @return 邀请小组信息
     */
    @RequestMapping(value = "/join", method = RequestMethod.GET)
    public R getGroupInvitationInfo(@RequestParam String code){
        GroupDto dto = groupService.getInvitationInfo(code);
        return dto != null ? R.success(dto) : R.fail(ResponseEnum.GROUP_INVITATION_CODE_INVALID);
    }

    /**
     * 加入小组
     * @param code 邀请码
     * @param request 请求对象
     * @return 加入结果
     */
    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public R joinGroup(@RequestParam String code, HttpServletRequest request){
        return groupService.joinGroup(code, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.GROUP_INVITATION_CODE_INVALID);
    }

    /**
     * 获取小组信息学习情况
     * @param groupId 小组ID
     * @param range 时间范围
     * @return 小组学习情况
     */
    @RequestMapping(value = "/{range}", method = RequestMethod.GET)
    public R getGroupInfo(@RequestParam Long groupId, @PathVariable String range){
        return R.success(groupService.getGroupInfo(groupId, range));
    }

    /**
     * 删除小组
     * @param groupId 小组ID
     * @param request 请求对象
     * @return 删除结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeGroup(@RequestParam Long groupId, HttpServletRequest request){
        return groupService.removeGroup(groupId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 退出小组
     * @param groupId 小组ID
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(value = "/exit", method = RequestMethod.DELETE)
    public R exitGroup(@RequestParam Long groupId, HttpServletRequest request){
        return groupService.exitGroup(groupId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改小组名称
     * @param groupName 小组新名称
     * @param groupId 小组ID
     * @param request 请求对象
     * @return 修改结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R renameGroup(@RequestParam String groupName, @RequestParam Long groupId, HttpServletRequest request){
        return groupService.renameGroup(groupName, groupId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
